<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button id="btn">点击</button>
    <script>
        class EventEmit {
            pond = {};
            //监听方法
            $on(eventName, func) {
                //判断是否有此事件
                if (!this.pond.hasOwnProperty(eventName)) {
                    this.pond[eventName] = []
                }
                //订阅的方法去重
                if (this.pond[eventName].some(item => item === func)) return;
                this.pond[eventName].push(func)
            }
            //执行方法
            $emit(eventName, ...args) {
                let funArr = this.pond[eventName] || [];
                //通知方法依次执行
                funArr.forEach(item => {
                    item.call(null, ...args)
                });
            }
        }
        let subscribe = new EventEmit()
        handle = (val) => {
            alert(val)
        }
        subscribe.$on('subscribefn', handle)//绑定事件 存放起来
        btn.addEventListener('click', function () {
            subscribe.$emit('subscribefn', 2)//调用存放里面的函数
        }, false)
    </script>
</body>

</html>